package Stream;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JLabel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.awt.Font;
import java.awt.Color;

public class MapList extends JInternalFrame {

	HashMap<String , MapData> map;
	
	private JTextField findStr;
	private JButton btnNewButton;
	private JButton button;
	private JButton btnNewButton_1;
	private JScrollPane scrollPane;
	private JTextArea result;
	private JLabel label;
	private JButton btnNewButton_2;
	private JLabel status;

	/**
	 * Create the frame.
	 */
	public MapList(HashMap<String, MapData> m) {
		super("List",true, true, true, false);
		this.map = m;
		setBounds(100, 100, 322, 329);
		getContentPane().setLayout(null);
		getContentPane().add(getFindStr());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getButton());
		getContentPane().add(getBtnNewButton_1());
		getContentPane().add(getScrollPane());
		getContentPane().add(getBtnNewButton_2());
		getContentPane().add(getStatus());

	}

	public void setMap(HashMap m) {
		this.map = m;
	}

	private JTextField getFindStr() {
		if (findStr == null) {
			findStr = new JTextField();
			findStr.setBounds(12, 10, 174, 21);
			findStr.setColumns(10);
		}
		return findStr;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("조회");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String str = findStr.getText();
					boolean flag = false;
					result.setText("");
					int cnt = 0;
					if (str.equals("")) {
						status.setText("검색어를 입력하세요.");
						return;
					}
					Collection<MapData> col = map.values();
					Iterator<MapData> it = col.iterator();//표준화된 출력을 위해사용
					while (it.hasNext()) {//BOF 부터 EOF까지
						MapData value = map.get(str);
						if (value == null) {
							JOptionPane.showMessageDialog(null, "검색에 실패하였습니다.");
							return;
						}
						if (value.getIrum().contains(str) || value.getPhone().contains(str) || value.getUserId().contains(str)) {
							flag = true;//검색에 성공
							result.append(value.toString());
							cnt++;
						}
					}
					if(flag){
						result.append(cnt+"건이 검색되었습니다.\n");
					}else{
						result.append("찾는 데이터가없습니다.\n");
					}
				}
			});
			btnNewButton.setBounds(198, 9, 97, 23);
		}
		return btnNewButton;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("모든 데이터");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					Set<String> st=map.keySet();
					Iterator<String> it = st.iterator();
					int cnt=0;
					while(it.hasNext()){
						String key = it.next();
						MapData value = map.get(key);
						result.append(value.toString());
						cnt++;
					}
					status.setText(cnt+"건이 검색되었습니다.");
//					Collection<MapData> col = map.value();
//					Iterator<MapData> it2 = col.iterator();
				}
			});
			button.setBounds(121, 42, 97, 23);
		}
		return button;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("모든 키");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					Set<String> st=map.keySet();
					Iterator<String> it = st.iterator();
					int cnt=0;
					while(it.hasNext()){
						String key = it.next();
						result.append(key+"\n");
						cnt++;
					}
					status.setText(cnt+"건이 검색되었습니다.");
				}
			});
			btnNewButton_1.setBounds(12, 42, 97, 23);
		}
		return btnNewButton_1;
	}
	
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 75, 283, 186);
			scrollPane.setViewportView(getResult());
			scrollPane.setColumnHeaderView(getLabel());
		}
		return scrollPane;
	}
	private JTextArea getResult() {
		if (result == null) {
			result = new JTextArea();
		}
		return result;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("아이디   성명          연락처                일자");
		}
		return label;
	}
	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton("청소");
			btnNewButton_2.setFont(new Font("문체부 제목 바탕체", Font.PLAIN, 12));
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					result.setText("");
				}
			});
			btnNewButton_2.setBounds(230, 42, 67, 23);
		}
		return btnNewButton_2;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("");
			status.setOpaque(true);
			status.setBackground(new Color(176, 224, 230));
			status.setBounds(12, 271, 283, 15);
		}
		return status;
	}
}
